using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WP.Commons;
using WP.DB;
namespace MaxLeaf
{
    public partial class frmMsgShow : Form
    {
        public frmMsgShow()
        {
            InitializeComponent();
        }

        public void ShowMsg(string strMsg)
        {
            DBAccess dbcon = new DBAccess(false, false);
            if (CvtFunc.ToString(DataFunctions.GetFieldValue("mUserDefault", "DefaultString", "userName='" + SysVar.UserName + "' and ProgName='frmAlertOption' and Name='chkUserAlert' and  Parameter='0'")).TrimEnd() == "0")
            {
                dbcon.CloseCon();  
                return;
            }
            frmAlertOption f = new frmAlertOption();
            DataView dv = dbcon.GetDataTable("select * from  mUserDefault where userName='" + SysVar.UserName + "' and ProgName='" + "frmAlertOption" + "'").DefaultView;

            dv.RowFilter = "Name='" + f.btnMsgWinBackColor.Name + "'";
            if (dv.Count == 1) this.BackColor = System.Drawing.Color.FromArgb(int.Parse((CvtFunc.ToString(dv[0]["DefaultString"]).Trim() == "" ? "0" : CvtFunc.ToString(dv[0]["DefaultString"]).Trim()), System.Globalization.NumberStyles.AllowHexSpecifier));

            dv.RowFilter = "Name='" + f.txtAlertWaitTime.Name + "'";
            if (dv.Count == 1)
            {
                int iSecond = CvtFunc.ToInt(DataFunctions.GetFieldValue("mUserDefault", "DefaultString", "userName='" + SysVar.UserName + "' and ProgName='frmAlertOption' and name='txtAlertWaitTime'"));
                int iMinute = CvtFunc.ToInt(DataFunctions.GetFieldValue("mUserDefault", "DefaultString", "userName='" + SysVar.UserName + "' and ProgName='frmAlertOption' and name='txtAlertRefreshTime'"));
                if (iSecond <= 0 || iSecond > iMinute *60 ) iSecond = 10;
                this.timer1.Interval = iSecond * 1000 / 100;
            }
            dbcon.CloseCon();  
            chkShowMsg.Checked = false; 
            lblMsg.Text = strMsg;
            this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Size.Width;
            this.Top = Screen.PrimaryScreen.WorkingArea.Height;// -this.Size.Height;
            this.TopLevel = true;
            this.Show(); 

        }

        private void frmMsgShow_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity - 0.01 > 0)
            {
                this.Opacity = this.Opacity - 0.01;
            }
            else
            {
                this.Close();
            }
        }

        private void frmMsgShow_MouseDown(object sender, MouseEventArgs e)
        {
            this.Close(); 
        }

        private void timerShow_Tick(object sender, EventArgs e)
        {
            while (this.Top > Screen.PrimaryScreen.WorkingArea.Height - this.Size.Height)
            {
                this.Top -= 1;
            }
        }

        private void frmMsgShow_FormClosing(object sender, FormClosingEventArgs e)
        {
            DBAccess dbcon = new DBAccess(false, false);
            string strSql = "Delete from mUserDefault where userName='" + SysVar.UserName + "' and ProgName='frmAlertOption' and Name='chkUserAlert' and  Parameter='0'";
            dbcon.ExecuteSql(strSql);

            strSql = "Insert into mUserDefault (UserName, ProgName,Parameter,Name,DefaultString) values ('" +
                SysVar.UserName + "','frmAlertOption','" + "0" + "','chkUserAlert','" + (chkShowMsg.Checked ? "0" : "1") + "')";
            dbcon.ExecuteSql(strSql);

        }

        private void lblMsg_Click(object sender, EventArgs e)
        {
            ReflectMethod.frmMainMenu_ShowUserAlert();
            this.Close(); 
        }
    }
}